home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / utilit~1 / futilsrc.zoo / fileutil / src / dd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-28  |  22.9 KB  |  891 lines

  1. /* dd -- convert a file while copying it.
  2.    Copyright (C) 1985, 1989-1991 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Written by Paul Rubin and David MacKenzie. */
  19.  
  20. /* Options:
  21.  
  22.    Numbers can be followed by a multiplier:
  23.    b=512, k=1024, w=2, xm=number m
  24.  
  25.    if=FILE            Read from FILE instead of stdin.
  26.    of=FILE            Write to FILE instead of stdout; don't
  27.                 truncate FILE.
  28.    ibs=BYTES            Read BYTES bytes at a time.
  29.    obs=BYTES            Write BYTES bytes at a time.
  30.    bs=BYTES            Override ibs and obs.
  31.    cbs=BYTES            Convert BYTES bytes at a time.
  32.    skip=BLOCKS            Skip BLOCKS ibs-sized blocks at
  33.                 start of input.
  34.    seek=BLOCKS            Skip BLOCKS obs-sized blocks at
  35.                 start of output.
  36.    count=BLOCKS            Copy only BLOCKS input blocks.
  37.    conv=CONVERSION[,CONVERSION...]
  38.  
  39.    Conversions:
  40.    ascii            Convert EBCDIC to ASCII.
  41.    ebcdic            Convert ASCII to EBCDIC.
  42.    ibm                Convert ASCII to alternate EBCDIC.
  43.    block            Pad newline-terminated records to size of
  44.                 cbs, replacing newline with trailing spaces.
  45.    unblock            Replace trailing spaces in cbs-sized block
  46.                 with newline.
  47.    lcase            Change uppercase characters to lowercase.
  48.    ucase            Change lowercase characters to uppercase.
  49.    swab                Swap every pair of input bytes.
  50.                 Unlike the Unix dd, this works when an odd
  51.                 number of bytes are read.
  52.    noerror            Continue after read errors.
  53.    sync                Pad every input block to size of ibs with
  54.                 trailing NULs. */
  55.  
  56. #include <stdio.h>
  57. #include <ctype.h>
  58. #ifdef STDC_HEADERS
  59. #define ISLOWER islower
  60. #define ISUPPER isupper
  61. #else
  62. #define ISLOWER(c) (isascii ((c)) && islower ((c)))
  63. #define ISUPPER(c) (isascii ((c)) && isupper ((c)))
  64. #endif
  65. #include <sys/types.h>
  66. #include <signal.h>
  67. #include "system.h"
  68.  
  69. #define equal(p, q) (strcmp ((p),(q)) == 0)
  70. #define max(a, b) ((a) > (b) ? (a) : (b))
  71.  
  72. /* Default input and output blocksize. */
  73. #define DEFAULT_BLOCKSIZE 512
  74.  
  75. /* Conversions bit masks. */
  76. #define C_ASCII 01
  77. #define C_EBCDIC 02
  78. #define C_IBM 04
  79. #define C_BLOCK 010
  80. #define C_UNBLOCK 020
  81. #define C_LCASE 040
  82. #define C_UCASE 0100
  83. #define C_SWAB 0200
  84. #define C_NOERROR 0400
  85. #define C_NOTRUNC 01000
  86. #define C_SYNC 02000
  87. /* Use separate input and output buffers, and combine partial input blocks. */
  88. #define C_TWOBUFS 04000
  89.  
  90. char *xmalloc ();
  91. SIGTYPE interrupt_handler ();
  92. int bit_count ();
  93. int parse_integer ();
  94. void apply_translations ();
  95. void copy ();
  96. void error ();
  97. void parse_conversion ();
  98. void print_stats ();
  99. void translate_charset ();
  100. void quit ();
  101. void scanargs ();
  102. void usage ();
  103.  
  104. /* The name this program was run with. */
  105. char *program_name;
  106.  
  107. /* The name of the input file, or NULL for the standard input. */
  108. char *input_file = NULL;
  109.  
  110. /* The input file descriptor. */
  111. int input_fd = 0;
  112.  
  113. /* The name of the output file, or NULL for the standard output. */
  114. char *output_file = NULL;
  115.  
  116. /* The output file descriptor. */
  117. int output_fd = 1;
  118.  
  119. /* The number of bytes in which atomic reads are done. */
  120. long input_blocksize = -1;
  121.  
  122. /* The number of bytes in which atomic writes are done. */
  123. long output_blocksize = -1;
  124.  
  125. /* Conversion buffer size, in bytes.  0 prevents conversions. */
  126. long conversion_blocksize = 0;
  127.  
  128. /* Skip this many records of `input_blocksize' bytes before input. */
  129. int skip_records = 0;
  130.  
  131. /* Skip this many records of `output_blocksize' bytes before output. */
  132. long seek_record = 0;
  133.  
  134. /* Copy only this many records.  <0 means no limit. */
  135. int max_record = -1;
  136.  
  137. /* Bit vector of conversions to apply. */
  138. int conversions_mask = 0;
  139.  
  140. /* Number of partial blocks written. */
  141. unsigned w_partial = 0;
  142.  
  143. /* Number of full blocks written. */
  144. unsigned w_full = 0;
  145.  
  146. /* Number of partial blocks read. */
  147. unsigned r_partial = 0;
  148.  
  149. /* Number of full blocks read. */
  150. unsigned r_full = 0;
  151.  
  152. /* Records truncated by conv=block. */
  153. unsigned r_truncate = 0;
  154.  
  155. /* Output representation of newline and space characters. */
  156. unsigned char newline_character = '\n';
  157. unsigned char space_character = ' ';
  158.  
  159. struct conversion
  160. {
  161.   char *convname;
  162.   int conversion;
  163. };
  164.  
  165. struct conversion conversions[] =
  166. {
  167.   "ascii", C_ASCII | C_TWOBUFS,    /* EBCDIC to ASCII. */
  168.   "ebcdic", C_EBCDIC | C_TWOBUFS,    /* ASCII to EBCDIC. */
  169.   "ibm", C_IBM | C_TWOBUFS,    /* Slightly different ASCII to EBCDIC. */
  170.   "block", C_BLOCK | C_TWOBUFS,    /* Variable to fixed length records. */
  171.   "unblock", C_UNBLOCK | C_TWOBUFS,    /* Fixed to variable length records. */
  172.   "lcase", C_LCASE | C_TWOBUFS,    /* Translate upper to lower case. */
  173.   "ucase", C_UCASE | C_TWOBUFS,    /* Translate lower to upper case. */
  174.   "swab", C_SWAB | C_TWOBUFS,    /* Swap bytes of input. */
  175.   "noerror", C_NOERROR,        /* Ignore i/o errors. */
  176.   "notrunc", C_NOTRUNC,        /* Do not truncate output file. */
  177.   "sync", C_SYNC,        /* Pad input records to ibs with NULs. */
  178.   NULL, 0
  179. };
  180.  
  181. /* Translation table formed by applying successive transformations. */
  182. unsigned char trans_table[256];
  183.  
  184. unsigned char ascii_to_ebcdic[] =
  185. {
  186.   0, 01, 02, 03, 067, 055, 056, 057,
  187.   026, 05, 045, 013, 014, 015, 016, 017,
  188.   020, 021, 022, 023, 074, 075, 062, 046,
  189.   030, 031, 077, 047, 034, 035, 036, 037,
  190.   0100, 0117, 0177, 0173, 0133, 0154, 0120, 0175,
  191.   0115, 0135, 0134, 0116, 0153, 0140, 0113, 0141,
  192.   0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
  193.   0370, 0371, 0172, 0136, 0114, 0176, 0156, 0157,
  194.   0174, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
  195.   0310, 0311, 0321, 0322, 0323, 0324, 0325, 0326,
  196.   0327, 0330, 0331, 0342, 0343, 0344, 0345, 0346,
  197.   0347, 0350, 0351, 0112, 0340, 0132, 0137, 0155,
  198.   0171, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
  199.   0210, 0211, 0221, 0222, 0223, 0224, 0225, 0226,
  200.   0227, 0230, 0231, 0242, 0243, 0244, 0245, 0246,
  201.   0247, 0250, 0251, 0300, 0152, 0320, 0241, 07,
  202.   040, 041, 042, 043, 044, 025, 06, 027,
  203.   050, 051, 052, 053, 054, 011, 012, 033,
  204.   060, 061, 032, 063, 064, 065, 066, 010,
  205.   070, 071, 072, 073, 04, 024, 076, 0341,
  206.   0101, 0102, 0103, 0104, 0105, 0106, 0107, 0110,
  207.   0111, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
  208.   0130, 0131, 0142, 0143, 0144, 0145, 0146, 0147,
  209.   0150, 0151, 0160, 0161, 0162, 0163, 0164, 0165,
  210.   0166, 0167, 0170, 0200, 0212, 0213, 0214, 0215,
  211.   0216, 0217, 0220, 0232, 0233, 0234, 0235, 0236,
  212.   0237, 0240, 0252, 0253, 0254, 0255, 0256, 0257,
  213.   0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
  214.   0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
  215.   0312, 0313, 0314, 0315, 0316, 0317, 0332, 0333,
  216.   0334, 0335, 0336, 0337, 0352, 0353, 0354, 0355,
  217.   0356, 0357, 0372, 0373, 0374, 0375, 0376, 0377
  218. };
  219.  
  220. unsigned char ascii_to_ibm[] =
  221. {
  222.   0, 01, 02, 03, 067, 055, 056, 057,
  223.   026, 05, 045, 013, 014, 015, 016, 017,
  224.   020, 021, 022, 023, 074, 075, 062, 046,
  225.   030, 031, 077, 047, 034, 035, 036, 037,
  226.   0100, 0132, 0177, 0173, 0133, 0154, 0120, 0175,
  227.   0115, 0135, 0134, 0116, 0153, 0140, 0113, 0141,
  228.   0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
  229.   0370, 0371, 0172, 0136, 0114, 0176, 0156, 0157,
  230.   0174, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
  231.   0310, 0311, 0321, 0322, 0323, 0324, 0325, 0326,
  232.   0327, 0330, 0331, 0342, 0343, 0344, 0345, 0346,
  233.   0347, 0350, 0351, 0255, 0340, 0275, 0137, 0155,
  234.   0171, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
  235.   0210, 0211, 0221, 0222, 0223, 0224, 0225, 0226,
  236.   0227, 0230, 0231, 0242, 0243, 0244, 0245, 0246,
  237.   0247, 0250, 0251, 0300, 0117, 0320, 0241, 07,
  238.   040, 041, 042, 043, 044, 025, 06, 027,
  239.   050, 051, 052, 053, 054, 011, 012, 033,
  240.   060, 061, 032, 063, 064, 065, 066, 010,
  241.   070, 071, 072, 073, 04, 024, 076, 0341,
  242.   0101, 0102, 0103, 0104, 0105, 0106, 0107, 0110,
  243.   0111, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
  244.   0130, 0131, 0142, 0143, 0144, 0145, 0146, 0147,
  245.   0150, 0151, 0160, 0161, 0162, 0163, 0164, 0165,
  246.   0166, 0167, 0170, 0200, 0212, 0213, 0214, 0215,
  247.   0216, 0217, 0220, 0232, 0233, 0234, 0235, 0236,
  248.   0237, 0240, 0252, 0253, 0254, 0255, 0256, 0257,
  249.   0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
  250.   0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
  251.   0312, 0313, 0314, 0315, 0316, 0317, 0332, 0333,
  252.   0334, 0335, 0336, 0337, 0352, 0353, 0354, 0355,
  253.   0356, 0357, 0372, 0373, 0374, 0375, 0376, 0377
  254. };
  255.  
  256. unsigned char ebcdic_to_ascii[] =
  257. {
  258.   0, 01, 02, 03, 0234, 011, 0206, 0177,
  259.   0227, 0215, 0216, 013, 014, 015, 016, 017,
  260.   020, 021, 022, 023, 0235, 0205, 010, 0207,
  261.   030, 031, 0222, 0217, 034, 035, 036, 037,
  262.   0200, 0201, 0202, 0203, 0204, 012, 027, 033,
  263.   0210, 0211, 0212, 0213, 0214, 05, 06, 07,
  264.   0220, 0221, 026, 0223, 0224, 0225, 0226, 04,
  265.   0230, 0231, 0232, 0233, 024, 025, 0236, 032,
  266.   040, 0240, 0241, 0242, 0243, 0244, 0245, 0246,
  267.   0247, 0250, 0133, 056, 074, 050, 053, 041,
  268.   046, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
  269.   0260, 0261, 0135, 044, 052, 051, 073, 0136,
  270.   055, 057, 0262, 0263, 0264, 0265, 0266, 0267,
  271.   0270, 0271, 0174, 054, 045, 0137, 076, 077,
  272.   0272, 0273, 0274, 0275, 0276, 0277, 0300, 0301,
  273.   0302, 0140, 072, 043, 0100, 047, 075, 042,
  274.   0303, 0141, 0142, 0143, 0144, 0145, 0146, 0147,
  275.   0150, 0151, 0304, 0305, 0306, 0307, 0310, 0311,
  276.   0312, 0152, 0153, 0154, 0155, 0156, 0157, 0160,
  277.   0161, 0162, 0313, 0314, 0315, 0316, 0317, 0320,
  278.   0321, 0176, 0163, 0164, 0165, 0166, 0167, 0170,
  279.   0171, 0172, 0322, 0323, 0324, 0325, 0326, 0327,
  280.   0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337,
  281.   0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347,
  282.   0173, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
  283.   0110, 0111, 0350, 0351, 0352, 0353, 0354, 0355,
  284.   0175, 0112, 0113, 0114, 0115, 0116, 0117, 0120,
  285.   0121, 0122, 0356, 0357, 0360, 0361, 0362, 0363,
  286.   0134, 0237, 0123, 0124, 0125, 0126, 0127, 0130,
  287.   0131, 0132, 0364, 0365, 0366, 0367, 0370, 0371,
  288.   060, 061, 062, 063, 064, 065, 066, 067,
  289.   070, 071, 0372, 0373, 0374, 0375, 0376, 0377
  290. };
  291.  
  292. void
  293. main (argc, argv)
  294.      int argc;
  295.      char **argv;
  296. {
  297.   int i;
  298.  
  299.   program_name = argv[0];
  300.  
  301.   /* Initialize translation table to identity translation. */
  302.   for (i = 0; i < 256; i++)
  303.     trans_table[i] = i;
  304.  
  305.   /* Decode arguments. */
  306.   scanargs (argc, argv);
  307.   apply_translations ();
  308.  
  309.   if (input_file != NULL)
  310.     {
  311.       input_fd = open (input_file, O_RDONLY);
  312.       if (input_fd < 0)
  313.     error (1, errno, "%s", input_file);
  314.     }
  315.   else
  316.     input_file = "standard input";
  317.  
  318.   if (input_fd == output_fd)
  319.     error (1, 0, "standard %s is closed",
  320.        input_fd == 0 ? "input" : "output");
  321.  
  322.   if (output_file != NULL)
  323.     {
  324.       int omode = O_RDWR | O_CREAT;
  325.  
  326.       if (seek_record == 0 && !(conversions_mask & C_NOTRUNC))
  327.     omode |= O_TRUNC;
  328.       output_fd = open (output_file, omode, 0666);
  329.       if (output_fd < 0)
  330.     error (1, errno, "%s", output_file);
  331. #ifndef FTRUNCATE_MISSING
  332.       if (seek_record > 0 && !(conversions_mask & C_NOTRUNC))
  333.     {
  334.       if (ftruncate (output_fd, seek_record * output_blocksize) < 0)
  335.         error (0, errno, "%s", output_file);
  336.     }
  337. #endif
  338.     }
  339.   else
  340.     output_file = "standard output";
  341.   
  342.   if (signal (SIGINT, SIG_IGN) != SIG_IGN)
  343.     signal (SIGINT, interrupt_handler);
  344.   copy ();
  345. }
  346.  
  347. void
  348. copy ()
  349. {
  350.   struct stat in_stats, out_stats;
  351.   register unsigned char c;    /* Each char being converted. */
  352.   register unsigned char savec, tempc; /* Copies of `c'. */
  353.   long in_offset = 0L;        /* Offset into input file for conv=swab. */
  354.   register int i;        /* Index into `ibuf'. */
  355.   int oc = 0;            /* Index into `obuf'. */
  356.   int pending_spaces = 0;    /* Number of pending blanks to add. */
  357.   int col = 0;            /* Index into each line. */
  358.   unsigned char *ibuf, *obuf;    /* Buffers. */
  359.   int nread, nwritten;        /* Byte counts for each block. */
  360.   int exit_status = 0;
  361.  
  362.   ibuf = (unsigned char *) xmalloc (input_blocksize);
  363.   if (conversions_mask & C_TWOBUFS)
  364.     obuf = (unsigned char *) xmalloc (output_blocksize);
  365.   else
  366.     obuf = ibuf;
  367.  
  368.   /* Use fstat instead of checking for errno == ESPIPE because
  369.      lseek doesn't work on some special files but doesn't return an
  370.      error, either. */
  371.   if (fstat (input_fd, &in_stats))
  372.     {
  373.       error (0, errno, "%s", input_file);
  374.       quit (1);
  375.     }
  376.  
  377.   if (S_ISREG (in_stats.st_mode) && skip_records > 0)
  378.     {
  379.       if (lseek (input_fd, skip_records * input_blocksize, SEEK_SET) < 0)
  380.     {
  381.       error (0, errno, "%s", input_file);
  382.       quit (1);
  383.     }
  384.     }
  385.   else
  386.     {
  387.       for (i = 0; i < skip_records; i++)
  388.     if (read (input_fd, ibuf, input_blocksize) < 0)
  389.       {
  390.         error (0, errno, "%s", input_file);
  391.         quit (1);
  392.       }
  393.       /* What if fewer bytes were read than requested? */
  394.     }
  395.  
  396.   if (fstat (output_fd, &out_stats))
  397.     {
  398.       error (0, errno, "%s", output_file);
  399.       quit (1);
  400.     }
  401.  
  402.   if (S_ISREG (out_stats.st_mode) && seek_record > 0)
  403.     {
  404.       if (lseek (output_fd, seek_record * output_blocksize, SEEK_SET) < 0)
  405.     {
  406.       error (0, errno, "%s", output_file);
  407.       quit (1);
  408.     }
  409.     }
  410.   else
  411.     {
  412.       for (i = 0; i < seek_record; i++)
  413.     if (read (output_fd, obuf, output_blocksize) < 0)
  414.       {
  415.         error (0, errno, "%s", output_file);
  416.         quit (1);
  417.       }
  418.       /* What if fewer bytes were read than requested? */
  419.     }
  420.   
  421.   if (max_record >= 0 && r_partial + r_full >= max_record)
  422.     return;
  423.  
  424.   while (1)
  425.     {
  426.       if (max_record >= 0 && r_partial + r_full >= max_record)
  427.     break;
  428.       if ((conversions_mask & C_SYNC) && (conversions_mask & C_NOERROR))
  429.     bzero (ibuf, input_blocksize);
  430.       nread = read (input_fd, ibuf, input_blocksize);
  431.       if (nread == 0)
  432.     break;
  433.       if (nread < 0)
  434.     {
  435.       error (0, errno, "%s", input_file);
  436.       if (conversions_mask & C_NOERROR)
  437.         {
  438.           print_stats ();
  439.           /* Seek past the bad block if possible. */
  440.           lseek (input_fd, input_blocksize, SEEK_CUR);
  441.           if (conversions_mask & C_SYNC)
  442.         nread = 0;
  443.           else
  444.         continue;
  445.         }
  446.       else
  447.         {
  448.           /* Write any partial block. */
  449.           exit_status = 2;
  450.           break;
  451.         }
  452.     }
  453.  
  454.       if (nread < input_blocksize)
  455.     {
  456.       r_partial++;
  457.       if (conversions_mask & C_SYNC)
  458.         {
  459.           if (conversions_mask & C_NOERROR)
  460.         nread = input_blocksize;
  461.           else
  462.         while (nread < input_blocksize)
  463.           ibuf[nread++] = '\0';
  464.         }
  465.     }
  466.       else
  467.     r_full++;
  468.  
  469.       if (ibuf == obuf)        /* If not C_TWOBUFS. */
  470.     {
  471.       nwritten = write (output_fd, obuf, nread);
  472.       if (nwritten != nread)
  473.         {
  474.           error (0, errno, "%s", output_file);
  475.           if (nwritten > 0)
  476.         w_partial++;
  477.           quit (1);
  478.         }
  479.       else if (nread == input_blocksize)
  480.         w_full++;
  481.       else
  482.         w_partial++;
  483.       continue;
  484.     }
  485.  
  486.       /* Copy to output, doing conversion and flushing output as necessary. */
  487.       for (i = 0; i < nread; i++)
  488.     {
  489.       c = trans_table[ibuf[i]];
  490.  
  491.       if (conversions_mask & C_SWAB)
  492.         {
  493.           if (++in_offset & 1)
  494.         {
  495.           /* Swap `c' and `savec'. */
  496.           tempc = c;
  497.           c = savec;
  498.           savec = tempc;
  499.           if (in_offset == 1)
  500.             continue;    /* No previous character to process. */
  501.         }
  502.         }
  503.  
  504.       if (conversions_mask & C_BLOCK) /* Pad with spaces. */
  505.         {
  506.           if (pending_spaces > 0)
  507.         {
  508.           pending_spaces--;
  509.           i--;        /* Push char back; get it later. */
  510.           c = space_character;
  511.         }
  512.           else if (c == newline_character)
  513.         {
  514.           pending_spaces = max (0, conversion_blocksize - col);
  515.           col = 0;
  516.           continue;
  517.         }
  518.           else if (col++ == conversion_blocksize)
  519.         {
  520.           r_truncate++;
  521.           continue;
  522.         }
  523.           else if (col - 1 > conversion_blocksize)
  524.         {
  525.           continue;
  526.         }
  527.         }
  528.       else if (conversions_mask & C_UNBLOCK) /* Strip trailing spaces. */
  529.         {
  530.           if (col++ >= conversion_blocksize)
  531.         {
  532.           col = pending_spaces = 0;
  533.           i--;        /* Push char back; get it later. */
  534.           c = newline_character;
  535.         }
  536.           else if (c == space_character) /* POSIX.2 wants "<blank>". */
  537.         {
  538.           pending_spaces++;
  539.           continue;
  540.         }
  541.           else if (pending_spaces > 0)
  542.         {
  543.           /* Hit the end of a run of spaces that were not at the
  544.              end of the conversion buffer.  Add them to the output. */
  545.           pending_spaces--;
  546.           col--;
  547.           i--;        /* Push char back; get it later. */
  548.           c = space_character;
  549.         }
  550.         }
  551.  
  552.       obuf[oc++] = c;
  553.       if (oc >= output_blocksize)
  554.         {
  555.           nwritten = write (output_fd, obuf, output_blocksize);
  556.           if (nwritten != output_blocksize)
  557.         {
  558.           error (0, errno, "%s", output_file);
  559.           if (nwritten > 0)
  560.             w_partial++;
  561.           quit (1);
  562.         }
  563.           else
  564.         w_full++;
  565.           oc = 0;
  566.         }
  567.     }
  568.     }
  569.  
  570.   if ((conversions_mask & C_SWAB) && in_offset > 0L)
  571.     {
  572.       obuf[oc++] = savec;
  573.       if (oc >= output_blocksize)
  574.     {
  575.       nwritten = write (output_fd, obuf, output_blocksize);
  576.       if (nwritten != output_blocksize)
  577.         {
  578.           error (0, errno, "%s", output_file);
  579.           if (nwritten > 0)
  580.         w_partial++;
  581.           quit (1);
  582.         }
  583.       else
  584.         w_full++;
  585.       oc = 0;
  586.     }
  587.     }
  588.  
  589.   if (conversions_mask & C_BLOCK)
  590.     {
  591.       /* The last record might need to be padded. */
  592.       while (pending_spaces-- > 0)
  593.     {
  594.       obuf[oc++] = space_character;
  595.       if (oc >= output_blocksize)
  596.         {
  597.           nwritten = write (output_fd, obuf, output_blocksize);
  598.           if (nwritten != output_blocksize)
  599.         {
  600.           error (0, errno, "%s", output_file);
  601.           if (nwritten > 0)
  602.             w_partial++;
  603.           quit (1);
  604.         }
  605.           else
  606.         w_full++;
  607.           oc = 0;
  608.         }
  609.     }
  610.     }
  611.   else if (conversions_mask & C_UNBLOCK)
  612.     {
  613.       /* Add a final '\n' if there are exactly `conversion_blocksize'
  614.      characters in the final record. */
  615.       if (col == conversion_blocksize)
  616.     obuf[oc++] = newline_character;
  617.     }
  618.  
  619.   /* Flush last block. */
  620.   if (oc > 0)
  621.     {
  622.       nwritten = write (output_fd, obuf, oc);
  623.       if (nwritten > 0)
  624.     w_partial++;
  625.       if (nwritten != oc)
  626.     {
  627.       error (0, errno, "%s", output_file);
  628.       quit (1);
  629.     }
  630.     }
  631.  
  632.   quit (exit_status);
  633. }
  634.  
  635. void
  636. scanargs (argc, argv)
  637.      int argc;
  638.      char **argv;
  639. {
  640.   int i, n;
  641.  
  642.   for (i = 1; i < argc; i++)
  643.     {
  644.       char *name, *val;
  645.  
  646.       name = argv[i];
  647.       val = index (name, '=');
  648.       if (val == NULL)
  649.     usage ("unrecognized option `%s'", name);
  650.       *val++ = '\0';
  651.  
  652.       if (equal (name, "if"))
  653.     input_file = val;
  654.       else if (equal (name, "of"))
  655.     output_file = val;
  656.       else if (equal (name, "conv"))
  657.     parse_conversion (val);
  658.       else
  659.     {
  660.       n = parse_integer (val);
  661.       if (n < 0)
  662.         error (1, 0, "invalid number `%s'", val);
  663.  
  664.       if (equal (name, "ibs"))
  665.         {
  666.           input_blocksize = n;
  667.           conversions_mask |= C_TWOBUFS;
  668.         }
  669.       else if (equal (name, "obs"))
  670.         {
  671.           output_blocksize = n;
  672.           conversions_mask |= C_TWOBUFS;
  673.         }
  674.       else if (equal (name, "bs"))
  675.         output_blocksize = input_blocksize = n;
  676.       else if (equal (name, "cbs"))
  677.         conversion_blocksize = n;
  678.       else if (equal (name, "skip"))
  679.         skip_records = n;
  680.       else if (equal (name, "seek"))
  681.         seek_record = n;
  682.       else if (equal (name, "count"))
  683.         max_record = n;
  684.       else
  685.         usage ("unrecognized option `%s=%s'", name, val);
  686.     }
  687.     }
  688.  
  689.   /* If bs= was given, both `input_blocksize' and `output_blocksize' will
  690.      have been set to non-negative values.  If either has not been set,
  691.      bs= was not given, so make sure two buffers are used. */
  692.   if (input_blocksize == -1 || output_blocksize == -1)
  693.     conversions_mask |= C_TWOBUFS;
  694.   if (input_blocksize == -1)
  695.     input_blocksize = DEFAULT_BLOCKSIZE;
  696.   if (output_blocksize == -1)
  697.     output_blocksize = DEFAULT_BLOCKSIZE;
  698.   if (conversion_blocksize == 0)
  699.     conversions_mask &= ~(C_BLOCK | C_UNBLOCK);
  700. }
  701.  
  702. /* Return the value of STR, interpreted as a non-negative decimal integer,
  703.    optionally multiplied by various values.
  704.    Return -1 if STR does not represent a number in this format. */
  705.  
  706. int
  707. parse_integer (str)
  708.      char *str;
  709. {
  710.   register int n = 0;
  711.   register int temp;
  712.   register char *p = str;
  713.  
  714.   while (isdigit (*p))
  715.     {
  716.       n = n * 10 + *p - '0';
  717.       p++;
  718.     }
  719. loop:
  720.   switch (*p++)
  721.     {
  722.     case '\0':
  723.       return n;
  724.     case 'b':
  725.       n *= 512;
  726.       goto loop;
  727.     case 'k':
  728.       n *= 1024;
  729.       goto loop;
  730.     case 'w':
  731.       n *= 2;
  732.       goto loop;
  733.     case 'x':
  734.       temp = parse_integer (p);
  735.       if (temp == -1)
  736.     return -1;
  737.       n *= temp;
  738.       break;
  739.     default:
  740.       return -1;
  741.     }
  742.   return n;
  743. }
  744.  
  745. /* Interpret one "conv=..." option. */
  746.  
  747. void
  748. parse_conversion (str)
  749.      char *str;
  750. {
  751.   char *new;
  752.   int i;
  753.  
  754.   do
  755.     {
  756.       new = index (str, ',');
  757.       if (new != NULL)
  758.     *new++ = '\0';
  759.       for (i = 0; conversions[i].convname != NULL; i++)
  760.     if (equal (conversions[i].convname, str))
  761.       {
  762.         conversions_mask |= conversions[i].conversion;
  763.         break;
  764.       }
  765.       if (conversions[i].convname == NULL)
  766.     {
  767.       usage ("%s: invalid conversion", str);
  768.       exit (1);
  769.     }
  770.       str = new;
  771.   } while (new != NULL);
  772. }
  773.  
  774. /* Fix up translation table. */
  775.  
  776. void
  777. apply_translations ()
  778. {
  779.   int i;
  780.  
  781. #define MX(a) (bit_count (conversions_mask & (a)))
  782.   if ((MX (C_ASCII | C_EBCDIC | C_IBM) > 1)
  783.       || (MX (C_BLOCK | C_UNBLOCK) > 1)
  784.       || (MX (C_LCASE | C_UCASE) > 1)
  785.       || (MX (C_UNBLOCK | C_SYNC) > 1))
  786.     {
  787.       error (1, 0, "\
  788. only one conv in {ascii,ebcdic,ibm}, {lcase,ucase}, {block,unblock}, {unblock,sync}");
  789.     }
  790. #undef MX
  791.  
  792.   if (conversions_mask & C_ASCII)
  793.     translate_charset (ebcdic_to_ascii);
  794.  
  795.   if (conversions_mask & C_UCASE)
  796.     {
  797.       for (i = 0; i < 256; i++)
  798.     if (ISLOWER (trans_table[i]))
  799.       trans_table[i] = toupper (trans_table[i]);
  800.     }
  801.   else if (conversions_mask & C_LCASE)
  802.     {
  803.       for (i = 0; i < 256; i++)
  804.     if (ISUPPER (trans_table[i]))
  805.       trans_table[i] = tolower (trans_table[i]);
  806.     }
  807.  
  808.   if (conversions_mask & C_EBCDIC)
  809.     {
  810.       translate_charset (ascii_to_ebcdic);
  811.       newline_character = ascii_to_ebcdic['\n'];
  812.       space_character = ascii_to_ebcdic[' '];
  813.     }
  814.   else if (conversions_mask & C_IBM)
  815.     {
  816.       translate_charset (ascii_to_ibm);
  817.       newline_character = ascii_to_ibm['\n'];
  818.       space_character = ascii_to_ibm[' '];
  819.     }
  820. }
  821.  
  822. void
  823. translate_charset (new_trans)
  824.      unsigned char *new_trans;
  825. {
  826.   int i;
  827.  
  828.   for (i = 0; i < 256; i++)
  829.     trans_table[i] = new_trans[trans_table[i]];
  830. }
  831.  
  832. /* Return the number of 1 bits in `i'. */
  833.  
  834. int
  835. bit_count (i)
  836.      register unsigned int i;
  837. {
  838.   register int set_bits;
  839.  
  840.   for (set_bits = 0; i != 0; set_bits++)
  841.     i &= i - 1;
  842.   return set_bits;
  843. }
  844.  
  845. void
  846. print_stats ()
  847. {
  848.   fprintf (stderr, "%u+%u records in\n", r_full, r_partial);
  849.   fprintf (stderr, "%u+%u records out\n", w_full, w_partial);
  850.   if (r_truncate > 0)
  851.     fprintf (stderr, "%u truncated block%s\n", r_truncate,
  852.          r_truncate == 1 ? "" : "s");
  853. }
  854.  
  855. void
  856. quit (code)
  857.      int code;
  858. {
  859.   int errcode = code ? code : 1;
  860.   print_stats ();
  861.   if (close (input_fd) < 0)
  862.     error (errcode, errno, "%s", input_file);
  863.   if (close (output_fd) < 0)
  864.     error (errcode, errno, "%s", output_file);
  865.   exit (code);
  866. }
  867.  
  868. SIGTYPE
  869. interrupt_handler ()
  870. {
  871.   quit (1);
  872. }
  873.  
  874. void
  875. usage (string, arg0, arg1)
  876.      char *string, *arg0, *arg1;
  877. {
  878.   fprintf (stderr, "%s: ", program_name);
  879.   fprintf (stderr, string, arg0, arg1);
  880.   fprintf (stderr, "\n");
  881.   fprintf (stderr, "\
  882. Usage: %s [if=file] [of=file] [ibs=bytes] [obs=bytes] [bs=bytes] [cbs=bytes]\n\
  883.        [skip=blocks] [seek=blocks] [count=blocks]\n\
  884.        [conv={ascii,ebcdic,ibm,block,unblock,lcase,ucase,swab,noerror,notrunc,\n\
  885.        sync}]\n\
  886. Numbers can be followed by a multiplier:\n\
  887. b=512, k=1024, w=2, xm=number m\n",
  888.        program_name);
  889.   exit (1);
  890. }
  891.